package com.siren.tank.fire;

import com.siren.tank.Dir;
import com.siren.tank.group.AbstractFactory;
import com.siren.tank.group.BaseTank;
import com.siren.tank.group.Bullet;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author : wangjichao
 * @description:
 * @date : 2020/7/28 22:06
 */
public class ThreeDirctionFire implements FireStrategy {

    @Override
    public void fire(BaseTank t) {
        int bx = t.getX() + BaseTank.WIDTH/2 - Bullet.WIDTH/2;
        int by = t.getY() + BaseTank.HEIGHT/2 - Bullet.HEIGHT/2;

        // 三面发射子弹，正面，左面，右面
        Dir[] values = Dir.values();
        List<Dir> dirs = new ArrayList<>(Arrays.asList(values));
        int i = dirs.indexOf(t.getDir());
        Dir left = i == 0 ? dirs.get(i + 4 - 1) : dirs.get(i - 1);
        Dir right = i == 3 ? dirs.get(i - 4 + 1):dirs.get(i + 1);
        AbstractFactory.INSTANCE.createBullet(bx, by, t.getDir(), t.getGroup());
        AbstractFactory.INSTANCE.createBullet(bx, by, left, t.getGroup());
        AbstractFactory.INSTANCE.createBullet(bx, by, right, t.getGroup());
    }
}
